package com.campus.backend.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.backend.bean.order.OrderQueryParam;
import com.campus.backend.entity.Order;
import com.campus.backend.entity.OrderGoods;
import com.campus.backend.mapper.OrderGoodsMapper;
import com.campus.backend.mapper.OrderMapper;
import com.campus.backend.vo.order.OrderVo;

import com.campus.common.bean.common.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-05-21
 */
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    /**
     * 分页查询订单列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    public PageInfo<OrderVo> getOrderListPage(OrderQueryParam param) {
        PageInfo<Order> pageInfo = PageHelper.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() -> {
                    LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(StrUtil.isNotBlank(param.getId()), Order::getId, param.getId())
                            .eq(param.getUserId() != null, Order::getUserId, param.getUserId())
                            .like(StrUtil.isNotBlank(param.getCustomerPhone()), Order::getCustomerPhone, param.getCustomerPhone())
                            .eq(param.getMerchantId() != null, Order::getMerchantId, param.getMerchantId())
                            .like(StrUtil.isNotBlank(param.getMerchantName()), Order::getMerchantName, param.getMerchantName())
                            .eq(param.getDeliveryId() != null, Order::getDeliveryId, param.getDeliveryId())
                            .eq(param.getStatus() != null, Order::getStatus, param.getStatus())
                            .eq(param.getMerchantProcess() != null, Order::getMerchantProcess, param.getMerchantProcess())
                            .eq(param.getDeliveryProcess() != null, Order::getDeliveryProcess, param.getDeliveryProcess())
                            .eq(param.getDeliveryType() != null, Order::getDeliveryType, param.getDeliveryType())
                            .like(StrUtil.isNotBlank(param.getReceiverName()), Order::getReceiverName, param.getReceiverName())
                            .like(StrUtil.isNotBlank(param.getReceiverPhone()), Order::getReceiverPhone, param.getReceiverPhone())
                            .ge(param.getStartTime() != null, Order::getCreateTime, param.getStartTime())
                            .le(param.getEndTime() != null, Order::getCreateTime, param.getEndTime())
                            .orderByDesc(Order::getCreateTime);
                    this.list(wrapper);
                });

        // 转换为VO
        List<OrderVo> orderVoList = pageInfo.getList().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());

        PageInfo<OrderVo> orderVoPageInfo = PageUtil.convertPageInfo(pageInfo, orderVoList);

        return orderVoPageInfo;
    }

    /**
     * 获取订单详情
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    public OrderVo getOrderDetail(String orderId) {
        Order order = this.getById(orderId);
        if (order == null) {
            return null;
        }
        return convertToVo(order);
    }

    /**
     * 转换为VO对象
     *
     * @param order 订单实体
     * @return 订单VO
     */
    private OrderVo convertToVo(Order order) {
        OrderVo orderVo = new OrderVo();
        BeanUtil.copyProperties(order, orderVo);

        // 查询订单商品列表
        LambdaQueryWrapper<OrderGoods> goodsWrapper = new LambdaQueryWrapper<>();
        goodsWrapper.eq(OrderGoods::getOrderId, order.getId());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectList(goodsWrapper);

        // 转换商品VO
        List<OrderVo.OrderGoodsVo> goodsVoList = orderGoodsList.stream()
                .map(goods -> {
                    OrderVo.OrderGoodsVo goodsVo = new OrderVo.OrderGoodsVo();
                    BeanUtil.copyProperties(goods, goodsVo);
                    return goodsVo;
                })
                .collect(Collectors.toList());

        orderVo.setGoods(goodsVoList);
        return orderVo;
    }
}
